package com.jch.basic.nio.client;

import com.jch.basic.nio.ChannelAdapter;
import com.jch.basic.nio.ChannelHandler;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NioClientHandler extends ChannelAdapter {

    public NioClientHandler(Selector selector, Charset charset) {
        super(selector, charset);
    }

    @Override
    protected void channelRead(ChannelHandler channelHandler, String s) {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "收到消息：" + s);
        channelHandler.writeAndFlush("NioClient 已收到消息！\r\n");
    }

    @Override
    protected void channelActive(ChannelHandler channelHandler) {
        try {
            System.out.println("连接LocalAddress：" + channelHandler.channel().getLocalAddress());
            channelHandler.writeAndFlush("NioClient to msg for you! \r\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
